<?php

include_once 'common.php';

$sql = '';

$sql = "INSERT INTO `trex_records`(`number`, `title`, `year`, `cycle`, `type`, `office`, `subjects`, `hours`, `pass`, `traineeNumber`, `traineeName`, `traineeDepartment`, `traineeOffice`, `traineeGroup`, `traineeGrade`, `traineePost`, `traineeLicences`, `traineeComments`, `coachNumber`, `coachName`, `subjectsComments`, `coachComments`, `failedSubjects`, `failedSubjectsCount`, `evaluations`, `evaluationMin`, `evaluationScore`, `trainingDate`, `history`, `status`, `commitTime`, `updateTime`) VALUES ";

$updateTime = date('Y-m-d H:i:s');

foreach ($payload as $key => $value) {
  $subjects = json_stringify($value->subjects);
  $traineeLicences = json_stringify($value->traineeLicences);
  $subjectsComments = json_stringify($value->subjectsComments);
  $failedSubjects = json_stringify($value->failedSubjects);
  $evaluations = json_stringify($value->evaluations);
  $history = json_stringify($value->history);
  $sql .= "(null,'{$value->title}',{$value->year},{$value->cycle},{$value->type},'{$value->office}','{$subjects}',{$value->hours},'{$value->pass}',{$value->traineeNumber},'{$value->traineeName}','','','','','','[]','',{$value->coachNumber},'{$value->coachName}','{$subjectsComments}','{$value->coachComments}','{$failedSubjects}',{$value->failedSubjectsCount},'{$evaluations}',{$value->evaluationMin},{$value->evaluationScore},'{$value->trainingDate}','{$history}',{$value->status},'{$value->commitTime}','{$updateTime}'),";
}
$sql = substr($sql, 0, -1) . ';';
$result = exec_sql($sql);
output(['result' => $result, 'sql' => $sql]);